﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Active.Core;

public partial class User_Controls_ContactListControl : System.Web.UI.UserControl
{
    public bool Editable
    {
        get
        {
            if (ViewState["Editable"] == null)
            {
                object obj = Session["CurrentObj"];
                ViewState["Editable"] = (bool)obj.GetType().GetMethod("CanEditObject").Invoke(null, null);
            }
            return (bool)ViewState["Editable"];
        }
    }

    private enum Views
    {
        MainView = 0,
        EditContactView = 1
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            ApplyAuthorizationRules();
        }
    }

    private void ApplyAuthorizationRules()
    {
        object obj = Session["CurrentObj"];
        this.Visible = !(bool)obj.GetType().GetProperty("IsNew").GetValue(obj, null);

        if (this.Visible)
        {
            this.lbtnAddContact.Visible =
                this.ContactListGridView.Columns[0].Visible =
                this.ContactListGridView.Columns[1].Visible = Editable;
        }
    }

    protected void lbtnAddContact_Click(object sender, EventArgs e)
    {
        this.MultiView1.ActiveViewIndex = (int)Views.EditContactView;
        this.ContactDetailsView.DefaultMode = DetailsViewMode.Insert;
    }

    protected void lbtnReturn_Click(object sender, EventArgs e)
    {
        this.MultiView1.ActiveViewIndex = (int)Views.MainView;
    }

    #region BusinessObject

    private Contacts _contacts;
    private Contacts GetContacts()
    {
        if (_contacts == null)
        {
            object businessObj = Session["CurrentObj"];
            if (businessObj != null)
            {
                try
                {
                    _contacts =
                        businessObj.GetType().GetProperty("ContactList").GetValue(businessObj, null) as Contacts;
                }
                catch
                {
                    return null;
                }
            }
            else
            {
                return null;
            }
        }
        return _contacts;
    }

    private int SaveContacts(Contacts obj)
    {
        int rowsAffected = 0;
        _contacts = obj.Save();
        rowsAffected = 1;
        return rowsAffected;
    }

    #endregion

    #region ContactDataSource

    protected void ContactDataSource_InsertObject(object sender, Csla.Web.InsertObjectArgs e)
    {
        Contacts obj = GetContacts();
        Contact contact = obj.AddNew();
        Csla.Data.DataMapper.Map(e.Values, contact);
        e.RowsAffected = SaveContacts(obj);

        Response.Redirect(Request.RawUrl, true);
    }

    protected void ContactDataSource_SelectObject(object sender, Csla.Web.SelectObjectArgs e)
    {
        Contacts obj = GetContacts();
        e.BusinessObject = 
            obj.GetContactById(int.Parse(this.ContactListGridView.SelectedDataKey.Value.ToString()));
    }

    protected void ContactDataSource_UpdateObject(object sender, Csla.Web.UpdateObjectArgs e)
    {
        Contacts obj = GetContacts();
        Contact contact = 
            obj.GetContactById(int.Parse(this.ContactListGridView.SelectedDataKey.Value.ToString()));
        Csla.Data.DataMapper.Map(e.Values, contact);
        e.RowsAffected = SaveContacts(obj);
        Response.Redirect(Request.RawUrl, true);
    }

    #endregion

    #region ContactsDataSource

    protected void ContactsDataSource_SelectObject(object sender, Csla.Web.SelectObjectArgs e)
    {
        e.BusinessObject = GetContacts();
    }

    protected void ContactsDataSource_DeleteObject(object sender, Csla.Web.DeleteObjectArgs e)
    {
        Contacts obj = GetContacts();
        obj.Remove(int.Parse(e.Keys["Id"].ToString()));
        e.RowsAffected = SaveContacts(obj);
    }

    #endregion
    protected void ContactListGridView_SelectedIndexChanged(object sender, EventArgs e)
    {
        this.MultiView1.ActiveViewIndex = (int)Views.EditContactView;
        this.ContactDetailsView.DefaultMode = DetailsViewMode.Edit;
    }
}
